/**
 * 工作编辑表单
 */
Ext.define('BenBen.modules.project.operation.OperationWorkEditForm', {
    extend: 'Ext.form.Panel',
    requires: [
       'BenBen.modules.system.organization.UserSearchWindow'
       
    ],
    border : false,
    bodyStyle : 'padding:5px',
    notExitMoreComponent : true, //更多组件是否不存在
    defaults : {
        margin: '5 5 5 5',
        anchor : '98%',
        labelWidth : 60,
        msgTarget :'side',
        labelAlign : 'right'
    },
    initComponent: function () {
    	var panel = this;
    	
    	var workOrigin = Ext.create('Ext.form.ComboBox',{
    		name: 'workOrigin',
            fieldLabel: '工作来源',
            mode : 'local',
            editable : false,
            labelAlign : 'right',
            value : '电话',
            triggerAction : 'all',
            displayField : 'name',
            valueField : 'name',
            store : new Ext.data.SimpleStore({
                fields : ['name'],
                data : [['电话'],['邮件'],['短信'],['微信'],['口头']]
            })
    	});
    	var trueName = Ext.create('Ext.form.TextField',{
            fieldLabel: '用户',
            labelAlign : 'right',
            allowBlank: false,
            blankText :'用户不能为空',
            name      :'trueName',
            listeners  : {  
                'focus':function(){  
                    var userWin =  Ext.create('BenBen.modules.system.organization.UserSearchWindow',{
                    	targetForm : panel,
                    	deviceStore :deviceStore
                    });
                    
                    userWin.show();
                }  
            }  
        });
    	//这个是账号
    	var username = Ext.create('Ext.form.Hidden',{
            fieldLabel: '用户',
            labelAlign : 'right',
            readOnly : true,
            name      :'username'
        });
    	var departmentName = Ext.create('Ext.form.TextField',{
    		fieldLabel: '部门',
    		labelAlign : 'right',
    		readOnly : true,
    		name      :'departmentName'
    	});
    	var departmentId = Ext.create('Ext.form.Hidden',{
    		fieldLabel: '部门',
    		labelAlign : 'right',
    		readOnly : true,
    		name      :'departmentId'
    	});
    	var startTime = Ext.create('BenBen.common.component.datetime.DateTimeField',{
    		fieldLabel: '日期',
    		labelAlign : 'right',
    		format:"Ymd",
    		value : new Date(),
    		name      :'startTime'
    	});
    	var phone = Ext.create('Ext.form.TextField',{
    		fieldLabel: '联系电话',
    		labelAlign : 'right',
    		readOnly : true,
    		name      :'phone'
    	});
    	var email = Ext.create('Ext.form.TextField',{
    		fieldLabel: 'Email',
    		labelAlign : 'right',
    		readOnly : true,
    		name      :'email'
    	});
    	var isEmail = Ext.create('Ext.form.ComboBox',{
    		name: 'msgType',
            fieldLabel: '信息方式',
            mode : 'local',
            editable : false,
            labelAlign : 'right',
            value : 2,
            triggerAction : 'all',
            displayField : 'name',
            valueField : 'number',
            store : new Ext.data.SimpleStore({
                fields : ['number','name'],
                data : [[0,'不发送信息'],[1,'短信'],[2,'邮件'],[3,'微信']]
            })
    	});
    	
        var commonSet = Ext.create('Ext.form.FieldSet', {
            title : '基础信息',
            autoHeight : true,
            layout : 'column',
            items : [{
                columnWidth : .33,
                layout : 'form',
                border : false,
                items : [workOrigin,startTime,isEmail]
            }, {
                columnWidth : .33,
                layout : 'form',
                border : false,
                items : [trueName,phone,username]
            }, {
                columnWidth : .33,
                layout : 'form',
                border : false,
                items : [departmentName,email,departmentId]
            }]
        });
        
        var workType = Ext.create('Ext.form.ComboBox',{
    		name: 'workType',
            fieldLabel: '工作类型',
            mode : 'local',
            editable : false,
            labelAlign : 'right',
            value : '服务',
            triggerAction : 'all',
            displayField : 'name',
            valueField : 'name',
            store : new Ext.data.SimpleStore({
                fields : ['name'],
                data : [['故障'],['服务']]
            })
    	});
        var hitchBigType = Ext.create('Ext.form.ComboBox',{
        	name: 'hitchBigType',
        	fieldLabel: '故障类型',
        	mode : 'local',
        	editable : false,
        	labelAlign : 'right',
        	value : '',
        	triggerAction : 'all',
        	displayField : 'name',
        	valueField : 'name',
        	store : new Ext.data.SimpleStore({
        		fields : ['name'],
        		data : [['网络'],['服务器'],['桌面电脑及外设']]
        	})
        });
        var hitchType = Ext.create('Ext.form.ComboBox',{
        	name: 'hitchType',
        	fieldLabel: '故障子类型',
        	mode : 'local',
        	editable : false,
        	labelAlign : 'right',
        	value : '电话',
        	triggerAction : 'all',
        	displayField : 'name',
        	valueField : 'name',
        	store : new Ext.data.SimpleStore({
        		fields : ['name'],
        		data : [['网络'],['服务器'],['桌面电脑及外设']]
        	})
        });
        var grade = Ext.create('Ext.form.ComboBox',{
    		name: 'grade',
            fieldLabel: '工作级别',
            mode : 'local',
            editable : false,
            labelAlign : 'right',
            value : 2,
            triggerAction : 'all',
            displayField : 'name',
            valueField : 'number',
            store : new Ext.data.SimpleStore({
                fields : ['number','name'],
                data : [[1,'重要'],[2,'普通']]
            })
    	});

        var deviceStore = Ext.create('Ext.data.Store',{
            autoLoad : false,
            fields: ['DETAIL','EQUI_ID'],
            proxy: {
                type: 'rest',
                actionMethods: {
                    create : 'POST',
                    read   : 'GET',
                    update : 'POST',
                    destroy: 'POST'
                },
                url : 'oa/grgemEquipments',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });
        var device = Ext.create('Ext.form.ComboBox',{
            name: 'device',
            fieldLabel : '设备',
            editable : false,
            labelAlign : 'right',
            value : '',
            triggerAction : 'all',
            displayField : 'EQUI_ID',
            valueField : 'EQUI_ID',
            store : deviceStore,
            queryMode : 'local',
            listeners: {
                select: function(combo, record, option) {
                    var detail = record[record.length - 1].data.DETAIL;
                    deviceConfigure.setValue(detail);
                }
            }
        });
        var deviceConfigure = Ext.create('Ext.form.TextArea',{
            name: 'deviceConfigure',
            labelAlign : 'right',
            fieldLabel: '配置信息'
        });
        var describe = Ext.create('Ext.form.TextArea',{
        	name: 'describe',
        	labelAlign : 'right',
        	fieldLabel: '工作描述'
        });
        var pic =  {  
            xtype: 'box', //或者xtype: 'component',  
            id: 'picUrl',  
            name: 'picUrl',  
            fieldLabel:  '报修图片',
            width: 600, //图片宽度  
            height: 400, //图片高度  
            autoEl: {  
                tag: 'img',    //指定为img标签  
                src: ''    //指定url路径  
            }  
        } ;
        var workSet = Ext.create('Ext.form.FieldSet', {
            title : '工作信息',
            autoHeight : true,
            layout : 'form',
            align: 'middle ',
            pack: 'center',
            items : [{
                layout : 'column',
                border : false,
                items : [{
                    columnWidth : .5,
                    layout : 'form',
                    border : false,
                    items : [workType,hitchBigType]
                }, {
                    columnWidth : .5,
                    layout : 'form',
                    border : false,
                    items : [grade,hitchType]
                }]
            },device,deviceConfigure,describe,pic]
        });
      
        Ext.define('model.operationWork', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'id'
            },{
                name: 'departmentName'
            },{
            	name: 'departmentId'
            },{
            	name: 'trueName'
            },{
            	name: 'username'
            },{
            	name: 'workOrigin'
            },{
            	name: 'startTime'
            },{
                name: 'phone'
            },{
                name: 'email'
            },{
                name: 'isEmail'
            },{
                name: 'workType'
            },{
                name: 'hitchBigType'
            },{
            	name: 'hitchType'
            },{
            	name: 'grade'
            },{
            	name: 'deviceConfigure'
            },{
            	name: 'state'
            },{
            	name: 'device'
            },{
            	name: 'describe'
            },{
            	name: 'picUrl'
            }]
        });
        Ext.apply(this,{
            border : false,
            layout : 'anchor',
            items :  [commonSet,workSet],
            reader :  Ext.create('Ext.data.JsonReader', {
                root : 'data',
                model : 'model.operationWork'
            })
        });
        this.pic = pic;
        this.callParent(arguments);
    },
    addMoreComponent : function(recordId){

        if(this.notExitMoreComponent){
        	Ext.define('dataModel', {
    			extend : 'Ext.data.Model',
    			fields : ['id','handleTime', 'duration', 'state','upTrueName','trueName','content'],
    			idProperty : 'id'
    		});
            var store = new Ext.create('Ext.data.Store', {
    			model : 'dataModel',
    			remoteSort : true,
    			proxy: {
    		        type: 'rest',
    		        actionMethods: {
    	                create : 'POST',
    	                read   : 'GET',
    	                update : 'POST',
    	                destroy: 'POST'
    	            },
                    url : 'project/operationWork/handles',
    		        reader: {
    		            type: 'json',
    		            root: 'data',
    		            totalProperty: 'totalCount'
    		        }
    		     }
    		});
            
            var gridPanel = Ext.create('Ext.grid.Panel', {
            	title : '处理步骤',
            	autoScroll : true,
                autoScroll : true,
                plugins: [{
                    ptype: 'rowexpander',
                    rowBodyTpl : [
                        '<p><b>处理内容:</b> {content}</p>'
                    ]
                }],
    			columns : [{
                    dataIndex: 'trueName',
                    text: '处理人'
                },{
                    dataIndex: 'upTrueName',
                    text: '上级处理人'
                },{
                    dataIndex: 'handleTime',
                    text: '处理时间',
                    width: 200
                },{
                	dataIndex: 'duration',
                	text: '处理时长'
                },{
                	dataIndex: 'state',
                	text: '状态',
                	renderer: function (value) {
                		if (value == '1') {
                			return "转移";
                		}else if (value == '2') {
                			return "处理中";
                		}else if (value == '3') {
                			return "挂起";
                		}else if (value == '4') {
                			return "已完成";
                		}
                	}
                }],
    			height: 300,
    			store : store,
    			loadMask : true,
    			viewConfig : {
    				stripeRows : true,
    				enableTextSelection : true
    			}
    		});
        	this.add(gridPanel);
        	this.store = store;
        }

        this.store.load({
        	params : {
        		workId : recordId
        	}
        });
        
        this.notExitMoreComponent = false;
    }
});
